const v = require('../Model/ConstValue');
import AudioUtils from "../Utils/AudioUtils";

cc.Class({
    extends: cc.Component,

    properties: {
        aniPre: {
            type: [cc.Prefab],
            default: []
        },
        effectLayer: {
            type: cc.Node,
            default: null
        },
        audioUtils: {
            type: AudioUtils,
            default: null
        }
    },

    // use this for initialization
    onLoad: function () {
        this.score = 0;
        this.setListener();
        this.lastTouchPos = cc.Vec2(-1, -1);
        this.isCanMove = true;
        this.isInPlayAni = false; // 是否在播放中
    },

    setController: function (controller) {
        this.controller = controller;
    },

    initWithCellModels: function (cellsModels) {
        this.cellViews = [];
        for (var i = 1; i <= v.GRID_ROWS; i++) {
            this.cellViews[i] = [];
            for (var j = 1; j <= v.GRID_COLUMNS; j++) {
                var type = cellsModels[i][j].type;
                var aniView = cc.instantiate(this.aniPre[type]);
                aniView.parent = this.node;
                var cellViewScript = aniView.getComponent("CellView");
                cellViewScript.initWithModel(cellsModels[i][j]);
                this.cellViews[i][j] = aniView;
            }
        }
    },

    setListener: function () {
        this.node.on(cc.Node.EventType.TOUCH_START, function (eventTouch) {
            if (this.isInPlayAni) {//播放动画中，不允许点击
                return true;
            }
            var touchPos = eventTouch.getLocation();
            var cellPos = this.convertTouchPosToCell(touchPos);
            if (cellPos) {
                var changeModels = this.selectCell(cellPos);
                this.isCanMove = changeModels.length < 3;
            }
            else {
                this.isCanMove = false;
            }
            return true;
        }, this);
        // 滑动操作逻辑
        this.node.on(cc.Node.EventType.TOUCH_MOVE, function (eventTouch) {
            if (this.isCanMove) {
                var startTouchPos = eventTouch.getStartLocation();
                var startCellPos = this.convertTouchPosToCell(startTouchPos);
                var touchPos = eventTouch.getLocation();
                var cellPos = this.convertTouchPosToCell(touchPos);
                if (startCellPos.x != cellPos.x || startCellPos.y != cellPos.y) {
                    this.isCanMove = false;
                    var changeModels = this.selectCell(cellPos);
                }
            }
        }, this);
        this.node.on(cc.Node.EventType.TOUCH_END, function (eventTouch) {
            // console.log("1111");
        }, this);
        this.node.on(cc.Node.EventType.TOUCH_CANCEL, function (eventTouch) {
            // console.log("1111");
        }, this);
    },

    // 根据点击的像素位置，转换成网格中的位置
    convertTouchPosToCell: function (pos) {
        pos = this.node.convertToNodeSpaceAR(pos);
        if (pos.x < 0 || pos.x >= v.GRID_WIDTH || pos.y < 0 || pos.y >= v.GRID_HEIGHT) {
            return false;
        }
        let y = Math.floor(pos.x / (v.OFFSET_X + v.CELL_WIDTH)) + 1;
        let x = v.GRID_ROWS - Math.floor(pos.y / (v.OFFSET_Y + v.CELL_HEIGHT));
        return cc.v2(x, y);
    },

    // 移动格子
    updateView: function (changeModels) {
        let newCellViewInfo = [];
        for (var i in changeModels) {
            var model = changeModels[i];
            var viewInfo = this.findViewByModel(model);
            var view = null;
            // 如果原来的cell不存在，则新建
            if (!viewInfo) {
                var type = model.type;
                var aniView = cc.instantiate(this.aniPre[type]);
                aniView.parent = this.node;
                var cellViewScript = aniView.getComponent("CellView");
                cellViewScript.initWithModel(model);
                view = aniView;
            }
            // 如果已经存在
            else {
                view = viewInfo.view;
                this.cellViews[viewInfo.x][viewInfo.y] = null;
            }
            var cellScript = view.getComponent("CellView");
            cellScript.updateView();// 执行移动动作
            if (!model.isDeath) {
                newCellViewInfo.push({
                    model: model,
                    view: view
                });
            }
        }
        // 重新标记this.cellviews的信息
        newCellViewInfo.forEach(function (ele) {
            let model = ele.model;
            this.cellViews[model.x][model.y] = ele.view;
        }, this);
    },

    // 显示选中的格子背景
    updateSelect: function (pos) {
        for (var i = 1; i <= v.GRID_ROWS; i++) {
            for (var j = 1; j <= v.GRID_COLUMNS; j++) {
                if (this.cellViews[i][j]) {
                    var cellScript = this.cellViews[i][j].getComponent("CellView");
                    if (pos.x == i && pos.y == j) {
                        cellScript.setSelect(true);
                    }
                    else {
                        cellScript.setSelect(false);
                    }

                }
            }
        }

    },

    // 根据cell的model返回对应的view
    findViewByModel: function (model) {
        for (var i = 1; i <= v.GRID_ROWS; i++) {
            for (var j = 1; j <= v.GRID_COLUMNS; j++) {
                if (this.cellViews[i][j] && this.cellViews[i][j].getComponent("CellView").model == model) {
                    return { view: this.cellViews[i][j], x: i, y: j };
                }
            }
        }
        return null;
    },

    getPlayAniTime: function (changeModels) {
        if (!changeModels) {
            return 0;
        }
        var maxTime = 0;
        changeModels.forEach(function (ele) {
            ele.cmd.forEach(function (cmd) {
                if (maxTime < cmd.playTime + cmd.keepTime) {
                    maxTime = cmd.playTime + cmd.keepTime;
                }
            }, this)
        }, this);
        return maxTime;
    },

    // 获得爆炸次数， 同一个时间算一个
    getStep: function (effectsQueue) {
        if (!effectsQueue) {
            return 0;
        }
        return effectsQueue.reduce(function (maxValue, efffectCmd) {
            return Math.max(maxValue, efffectCmd.step || 0);
        }, 0);
    },

    //一段时间内禁止操作
    disableTouch: function (time, step) {
        if (time <= 0) {
            return;
        }
        this.isInPlayAni = true;
        this.node.runAction(cc.sequence(cc.delayTime(time), cc.callFunc(function () {
            this.isInPlayAni = false;
            this.audioUtils.playContinuousMatch(step);
        }, this)));
    },

    // 正常击中格子后的操作
    selectCell: function (cellPos) {
        var result = this.controller.selectCell(cellPos); // 直接先丢给model处理数据逻辑
        var changeModels = result[0]; // 有改变的cell，包含新生成的cell和生成马上摧毁的格子
        var effectsQueue = result[1]; //各种特效
        this.playEffect(effectsQueue);
        this.disableTouch(this.getPlayAniTime(changeModels), this.getStep(effectsQueue));
        this.updateView(changeModels);
        this.controller.cleanCmd();
        if (changeModels.length >= 2) {
            this.updateSelect(cc.v2(-1, -1));
            this.audioUtils.playSwap();
        }
        else {
            this.updateSelect(cellPos);
            this.audioUtils.playClick();
        }
        return changeModels;
    },

    playEffect: function (effectsQueue) {
        this.effectLayer.getComponent("EffectLayer").playEffects(effectsQueue);
    },

});
